﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KhaoSatSinhVienTotNghiep.Model
{
   public partial class User
    {
       private static KhaoSatSVTNDataContext context = new KhaoSatSVTNDataContext();
       /// <summary>
       /// Lấy tất cả User có trong CSDL
       /// </summary>
       /// <returns></returns>
       public static List<User> GetAll()
       {
           return context.Users.ToList();
       }

        /// <summary>
        /// Lấy user theo username
        /// </summary>
        /// <returns></returns>
        /// 
       public static User GetByUserName(string username)
       {
           return context.Users.Where(us => us.UserName == username).FirstOrDefault();
       }

       /// <summary>
       /// Kiểm tra trùng
       /// </summary>
       /// <returns></returns>
       /// 
       public static bool CheckExist(string user)
       {
           var kq = context.Users.Where(us => us.UserName == user);
           if (kq.Count() > 0)
               return true;
           return false;
       }

        /// <summary>
        /// Thêm mới một user
        /// </summary>
        /// <returns></returns>
        /// 
       public static bool AddNew(User us)
       {
           if (CheckExist(us.UserName) == true)
               return false;
           else 
           {
               context.Users.InsertOnSubmit(us);
               context.SubmitChanges();
               return true;
           }
       }

        /// <summary>
        /// Update User
        /// </summary>
        /// <returns></returns>
        /// 
       public static bool Update(User us)
       {
           if (CheckExist(us.UserName) == true)
           {
               User user = GetByUserName(us.UserName);
               user.Password = us.Password;
               user.HoTen = us.HoTen;
               user.NgaySinh = us.NgaySinh;
               user.GioiTinh = us.GioiTinh;
               user.Email = us.Email;
               user.DienThoai = us.DienThoai;
               user.DiaChi1 = us.DiaChi1;
               context.SubmitChanges();
               return true;
           }
           else
               return false;
       }
    }
}
